FHDoc = (function() {
	function textNode(element, text) {
		if (text['prop'] === '#text') {
				element.innerText = text['value']
			} else {
				element.innerText = text.toString()
			}
	}
	function createTrTh() {
		texts = Array.apply(null, arguments)
		var tr = document.createElement('tr')
		for (var i = 0, length = texts.length; i < length; i += 1) {
			var th = document.createElement('th')
			textNode(th, texts[i])
			tr.appendChild(th)
		}
		return tr
	}
	function createTrTd() {
		texts = Array.apply(null, arguments)
		var tr = document.createElement('tr')
		for (var i = 0, length = texts.length; i < length; i += 1) {
			var td = document.createElement('td')
			textNode(td, texts[i])
			tr.appendChild(td)
		}
		return tr
	}
	function createUl() {
		texts = Array.apply(null, arguments)
		var ul = document.createElement('ul')
		for (var i = 0, length = texts.length; i < length; i += 1) {
			var li = document.createElement('li')
			textNode(li, texts[i])
			ul.appendChild(li)
		}
		return ul
	}
	/*|读多行
	|*|@param {number} line 读到的行数
	|*|@param {array} sentence 写入的句子
	|*|@param {array} content 原始内容
	|*|@param {function} requirement 条件
	|*/
	function readMoreLine(line, sentence, content, requirement) {
		requirement = requirement || function() {
				return typeof content[line] !== 'undefined' && content[line][0] !== '@'
			}
		while (requirement()) {
			sentence.push({'prop': '#text', 'value': content[line]})
			line += 1
		}
	}
	function tree2HTML(tree) {
		var div = document.createElement('div')
		div.classList.add('FHDoc')
		for (var i = 0, length = tree.length; i < length; i += 1) {
			var object
			if (tree[i]['prop'] === '#text') {
				object = document.createElement('p')
				object.classList.add('describe')
				object.innerText = tree[i]['value']
			} else if (tree[i]['prop'] === 'param') {
				object = document.createElement('table')
				thead = document.createElement('thead')
				var tr = document.createElement('tr')
				var th = document.createElement('th')
				th.innerText = '传入参数'
				th.setAttribute('colspan', 3)
				tr.appendChild(th)
				thead.appendChild(tr)
				tr = null
				th = null
				thead.appendChild(createTrTh('类型', '名称', '描述'))
				object.appendChild(thead)
				tbody = document.createElement('tbody')
				while (typeof tree[i] !== 'undefined' && tree[i]['prop'] === 'param') {
						tbody.appendChild(createTrTd(tree[i]['type'] ,tree[i]['keyword'] , tree[i]['describe']))
						i += 1
				}
				i -= 1
				object.appendChild(tbody)
			} else if (tree[i]['prop'] === 'return') {
				object = document.createElement('p')
				object.classList.add('return')
				object.innerText = tree[i]['type']
				span = document.createElement('span')
				span.classList.add('describe')
				span.innerText = tree[i]['describe']
				object.appendChild(span)
			} else if (tree[i]['prop'] === 'example') {
				p = document.createElement('p')
				p.classList.add('example')
				div.appendChild(p)
				p.innerText = tree[i]['describe']
				object = document.createElement('pre')
				code = document.createElement('code')
				for (var j = 0, depth = tree[i]['code'].length; j < depth; j += 1) {
					code.innerHTML += codeFormat(tree[i]['code'][j]['value']) + '\n'
				}
				object.appendChild(code)
			} else if (tree[i]['prop'] === 'tips') {
				object = document.createElement('div')
				p = document.createElement('p')
				p.classList.add('tips')
				p.innerText = tree[i]['describe']
				object.appendChild(p)
				object.appendChild(createUl.apply(null, tree[i]['tips']))
			}
			div.appendChild(object)
		}
		return div
	}
	function parse(content) {
		var readLines = function(where) {
			return readMoreLine(i += 1, sentence[where], content)
		}
		var catContent = function (num) {
			return current.slice(current[num] === '-'? num + 1: num).join(' ') || '-'
		}
		var result = []
		content = content.trim().substring(3, content.length - 4).replace(/\|\*\|| \* /g, '').trim().split('\n')
		for (var i = 0, length = content.length; i < length; i += 1) {
			var current = content[i]
			if (current[0] === '@') {
				current = current.replace(/ +/g, ' ').split(' ')
				var sentence = {}
				var prop = current[0].substring(1)
				sentence['prop'] = prop
				if (prop === 'param') {
					sentence['type'] = current[1].substring(1, current[1].length - 1)
					sentence['keyword'] = current[2]
					sentence['describe'] = catContent(3)
				} else if (/return[s]?/.test(prop)) {
					sentence['type'] = current[1].substring(1, current[1].length - 1)
					sentence['describe'] = catContent(2)
				} else if (prop === 'example') {
					sentence['describe'] = catContent(2)
					sentence['code'] = []
					readLines('code')
				} else if (prop === 'tips' || prop === 'tip') {
					sentence['prop'] = 'tips'
					sentence['describe'] = catContent(2)
					sentence['tips'] = []
					readLines('tips')
				}
				result.push(sentence)
			} else {
				result.push({'prop': '#text', 'value': current})
			}
		}
		return result
	}
	function codeFormat(content) {
		content = content || '-'
		content = content.trim()
		var pattern = []
		var result = []
		pattern['number'] = /[0-9]+/g
		pattern['function'] = /([A-Za-z]+)\(([\S ]*)\)/g
		for (var prop in pattern) {
			var current = content.match(pattern[prop])
			console.log(current)
			if (current) {
				for (var i = 0, length = current.length; i < length; i += 1) {
					var object = {}
					var span = document.createElement('span')
					span.classList.add('FHDoc-' + prop)
					span.innerText = current[i]
					object['raw'] = current[i]
					object['value'] = span.outerHTML
					result.push(object)
				}
			}
		}
		for (var i = 0, length = result.length; i < length; i += 1) {
			content = content.replace(result[i]['raw'], result[i]['value'])
		}
		return content
	}
	return {
		tree2HTML: tree2HTML,
		parse: parse,
		codeFormat: codeFormat
}
})()